package indi.cloud.common.elasticsearch.core;

import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;

import java.util.List;

/**
 * @author yangyifan
 * @version 1.0
 * @date 2024/3/12 17:45
 * @Description
 */
public class EsUtils {
    private BoolQueryBuilder boolQueryBuilder;

    public EsUtils(BoolQueryBuilder boolQueryBuilder) {
        this.boolQueryBuilder = boolQueryBuilder;
    }

    /**
     * 生成es 区间检索结构体
     *
     * @param filedName 需要检索的es字段key
     * @param start     区间开始值
     * @param end       区间结束值
     * @return
     */
    public EsUtils range(String filedName, String start, String end) {
        if (StringUtils.isBlank(start) && StringUtils.isBlank(end)) {
            return this;
        } else if (StringUtils.isNotBlank(start) && StringUtils.isBlank(end)) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(filedName).from(start));
            return this;
        } else if (StringUtils.isNotBlank(end) && StringUtils.isBlank(start)) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(filedName).to(end));
            return this;
        } else {
            boolQueryBuilder.must(QueryBuilders.rangeQuery(filedName).from(start).to(end));
            return this;
        }

    }

    /**
     * es 相等条件
     *
     * @param filedName 需要检索的es字段key
     * @param value     值
     * @return
     */
    public EsUtils equals(String filedName, String value) {
        if (value == null || StringUtils.isBlank(value)) {
            return this;
        }
        MatchQueryBuilder query = QueryBuilders.matchQuery(filedName, value);
        boolQueryBuilder.must(query);
        return this;
    }


    /**
     * es 类mysql in查询
     *
     * @param filedName 需要检索的es字段key
     * @param value     值
     * @return
     */
    public EsUtils in(String filedName, Object value) {
        if (value == null || StringUtils.isBlank(value.toString())) {
            return this;
        }
        if (value instanceof String) {
            List<String> valueList = Splitter.on(",").splitToList(value.toString());
            TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder(filedName, valueList.toArray(new String[0]));
            if (valueList.size() > 0) {
                boolQueryBuilder.must(termsQueryBuilder);
            }
        }
        return this;
    }


//    /**
//     * es 类mysql in查询
//     *
//     * @param filedName 需要检索的es字段key
//     * @param value     值
//     * @return
//     */
//    public EsUtils in(String filedName, Object value) {
//        if (value == null||StringUtils.isBlank(value.toString())) {
//            return this;
//        }
//        if (value instanceof String) {
//            List<String> valueList = Splitter.on(",").splitToList(value.toString());
//            BoolQueryBuilder exersiceBoolQuery = QueryBuilders.boolQuery();
//            for (String record : valueList) {
//                exersiceBoolQuery.should(QueryBuilders.termQuery(filedName, record));
//            }
//            if (exersiceBoolQuery.should().size() > 0) {
//                boolQueryBuilder.must(exersiceBoolQuery);
//            }
//        }
//        return this;
//    }

    /**
     * 构建最终查询结构体
     * 结束
     *
     * @return
     */
    public Query build() {
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        queryBuilder.withQuery(boolQueryBuilder);
        Query query = queryBuilder.build();
        return query;
    }

    public Query scrollBuild() {
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        queryBuilder.withQuery(boolQueryBuilder);
        Query query = queryBuilder.build();
        return query;
    }
}
